<?php

namespace App\Models;


use Illuminate\Support\Facades\Cache;


//港口表
class CustomsHarbour extends BaseModel
{
    protected $table = 'yt_customs_harbour';

    public $timestamps = false;

    protected $fillable = [
        'name' ,
        'type',
        'country',
        'city',
    ];

    const START_PORT = 1;
    const DESC_PORT = 2;

    const TYPE = [
        self::START_PORT => '起运港',
        self::DESC_PORT => '目的港',
    ];





    // 获取国家列表
    static function getCountryByType($type): array
    {
        return self::query()->where('type', $type)->distinct()->pluck('country','country')
            ->toArray() ?: [];
    }

    public static function getFromHarbourList()
    {
        return Cache::Remember('getFromHarbour:' , config('constants.CACHE_EXPIRES'), function () {
            $fromList = self::where(['type' => self::START_PORT])->pluck('name', 'id')->toArray();
            return $fromList;
        });
    }

    public static function getDestHarbourList()
    {
        return Cache::Remember('getDestHarbour:' , config('constants.CACHE_EXPIRES'), function () {
            $destList = self::where(['type' => self::DESC_PORT])->pluck('name', 'id')->toArray();
            return $destList;
        });
    }


    // 获取
    public static function getByName($name, $type)
    {
        return Cache::Remember('getHarbourByNameType:'.$type. $name , config('constants.CACHE_EXPIRES'), function ()
            use($name, $type) {
            return self::where(['name' => $name, 'type' => $type])->first();
        });
    }



}
